package com.cda.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cda.mall.dao.ProductImgMapper;
import com.cda.mall.dao.ProductParamsMapper;
import com.cda.mall.dao.ProductSkuMapper;
import com.cda.mall.entity.*;
import com.cda.mall.dao.ProductMapper;
import com.cda.mall.service.IProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cda.mall.vo.ResStatus;
import com.cda.mall.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品 商品信息相关表：分类表，商品图片表，商品规格表，商品参数表 服务实现类
 * </p>
 *
 * @author cda
 * @since 2021-11-13
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductImgMapper productImgMapper;

    @Autowired
    private ProductSkuMapper productSkuMapper;

    @Autowired
    private ProductParamsMapper productParamsMapper;

    @Override
    public ResultVO listRecommendProducts() {
        List<ProductVO> productVOS = productMapper.selectRecommendProducts();
        return new ResultVO(ResStatus.OK,"success",productVOS);
    }

    @Override
    public ResultVO getProductBasicInfo(String productId) {
        //1.商品基本信息
        QueryWrapper<Product> wrapper = new QueryWrapper();
        wrapper.eq("product_status",1);
        wrapper.eq("product_id",productId);
        List<Product> list = productMapper.selectList(wrapper);
        if (list.size()>0){
            //2.商品图片
            QueryWrapper<ProductImg> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("item_id",productId);
            List<ProductImg> list1 = productImgMapper.selectList(wrapper1);
            //3.商品套餐
            QueryWrapper<ProductSku> wrapper2 = new QueryWrapper<>();
            wrapper2.eq("product_id",productId);
            wrapper2.eq("status",1);
            List<ProductSku> list2 = productSkuMapper.selectList(wrapper2);
            //数据封装：HashMap
            HashMap<String,Object> basicInfo = new HashMap<>();
            basicInfo.put("product",list.get(0));
            basicInfo.put("productImgs",list1);
            basicInfo.put("productSkus",list2);
            return new ResultVO(ResStatus.OK,"success",basicInfo);
        }else {
            return new ResultVO(ResStatus.NO,"查询的商品不存在",null);
        }



    }

    @Override
    public ResultVO getProductParamsById(String productId) {
        QueryWrapper<ProductParams> wrapper = new QueryWrapper();
        wrapper.eq("product_id",productId);
        ProductParams productParams = productParamsMapper.selectOne(wrapper);
        if (productParams != null){
            return new ResultVO(ResStatus.OK,"success",productParams);
        }else {
            return new ResultVO(ResStatus.NO,"此商品可能为三无产品",null);
        }
    }
}
